﻿#include "LogicCampModule.h"
#include "LogicService.h"

#include "base/core/com_define.h"
#include "base/core/os/time.h"
#include "base/core/Profiler.h"
#include "base/core/gamemath.h"
#include "Common/Utility/ServerTime.h"
#include "ProtoBuffer/AllProtocol.h"
#include "Access/CharacterDataSet.h"
#include "Character/PlayerMgr.h"
#include "Common/Utility/AccountUtility.h"
#include "Mail/GMailManager.h"
#include "Common/Item/ItemMgr.h"
#include "Common/TableData/MailMailCfg.h"
#include "Common/CampDefine.h"
#include "Common/TableData/CampKnighthoodCfg.h"
#include "Character/MissionPart.h"
#include "base/core/Utf8AndWideChar.h"
#include "Character/npc/Npc.h"
#include "Common/TableData/ConstantConstantCfg.h"
#include "Common/TableData/CampFeteCfg.h"
#include "Common/TableData/FunctionunlockFunctionUnlockCfg.h"
#include "Common/AttrMgr/AttrMgr.h"
#include "Character/HuoYueDuPart.h"
#include "Common/TableData/ArenaRobotCfg.h"
#include "Common/TableData/CampConstantCfg.h"
#include "Character/drop/DropMgr.h"

#ifdef _MSC_VER
#pragma warning(disable:4100)
#pragma warning(disable:4244)
#pragma warning(disable:4102)
#endif

using namespace ProtoBuf;

uint32_t LogicCampModule::camp_open_level = 0;
uint32_t LogicCampModule::CampLightLeaderNpc = 0;
uint32_t LogicCampModule::CampDarkLeaderNpc = 0;

LogicCampModule::LogicCampModule()
{}

LogicCampModule::~LogicCampModule()
{}


bool LogicCampModule::Init()
{
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}
	pMessageDispatch->registerServerHandler(EMODULE_ID_CAMP, this);
	pMessageDispatch->registerClientHandler(EMODULE_ID_CAMP, this);

	g_GetEvent()->Subscribe(this, EVENT_CREATURE_DEAD, 0, CREATURE_PLAYER, "LogicCampModule::Init");

	g_GetEvent()->Subscribe(this, EVENT_LEVELUP, 0, CREATURE_PLAYER, "LogicCampModule::Init");
	g_GetEvent()->Subscribe(this, EVENT_ADD_ATT_POINT, 0, CREATURE_PLAYER, "LogicCampModule::Init");


	const FunctionunlockFunctionUnlockCfgInfo * pOpenCamp = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_CAMP);
	if (nullptr == pOpenCamp)
	{
		LogErrFmtPrint("[logic] CenterToLogic_ChangeCampFlag pOpenCamp = nullptr.. failed....");
		return false;
	}

	const ConstantConstantCfgInfo *pCampLightLeaderNpcCfg = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_CampLightLeaderNpc);
	if (nullptr == pCampLightLeaderNpcCfg)
	{
		LogErrFmtPrint("[logic]CampMgr::Init pCampLightLeaderNpcCfg = nullptr.. failed....");
		return false;
	}
	const ConstantConstantCfgInfo *pCampDarkLeaderNpcCfg = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_CampDarkLeaderNpc);
	if (nullptr == pCampDarkLeaderNpcCfg)
	{
		LogErrFmtPrint("[logic]CampMgr::Init pCampDarkLeaderNpcCfg = nullptr.. failed....");
		return false;
	}

	camp_open_level = pOpenCamp->unlockLevel;
	CampLightLeaderNpc = pCampLightLeaderNpcCfg->constantdata;
	CampDarkLeaderNpc = pCampDarkLeaderNpcCfg->constantdata;

	return true;
}

void LogicCampModule::UnInit()
{
	g_GetEvent()->UnSubscribeAll(this);
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return;
	}
	pMessageDispatch->unregisterServerHandler(EMODULE_ID_CAMP);
}

void LogicCampModule::onClientMessage(uint32_t actionId, RECV_PACKAGE &package, uint64_t Id /*= 0*/)
{
	switch (actionId)
	{
	case CLIENT_TO_SERVER_CAMP_PRAY_INFO:
	{
		ClientToLogic_CampPrayAttInfo(Id, package);
	}
	break;
	case CLIENT_TO_CENTER_CAMP_PRAY:
	{
		ClientToLogic_CampPrayReq(Id, package);
	}
	break;
	default:
		break;
	}
}

void LogicCampModule::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	switch (actionId)
	{
	case CENTER_TO_LOGIC_CAMP_CHAR_INFO_NOTIFY:
		CenterToLogic_NotifyCampCharInfo(package);
		break;
	case CENTER_TO_LOGIC_CAMP_CHECK_NOTICE_COST_REQ:
		CenterToLogic_CampCheckNoticeCost(package);
		break;
	case CENTER_TO_LOGIC_CAMP_CHECK_ELECTION_COST_REQ:
		CenterToLogic_CampCheckElectionCost(package);
		break;
	case CENTER_TO_LOGIC_CAMP_CHECK_VOTE_REQ:
		CenterToLogic_CampCheckVote(package);
		break;
	case CENTER_TO_LOGIC_CAMP_CHECK_PRAY_COST_REQ:
		CenterToLogic_CampCheckPrayCost(package);
		break;
	case CENTER_TO_LOGIC_CAMP_TOTIFY_ADD_STH:
		CenterToLogic_CampAddSth(package);
		break;
	case CENTER_TO_LOGIC_CAMP_CHANGE_CAMP_FLAG:
		CenterToLogic_ChangeCamp(package);
		break;
	case CENTER_TO_LOGIC_CAMP_CHANGE_CAMP_LEADER:
		CenterToLogic_ChangeCampLeader(package);
		break;
	case  CENTER_TO_LOGIC_CAMP_CAMP_ATT_CHANGE:
		CenterToLogic_CampAttChange(package);
		break;
	case  CENTER_TO_LOGIC_CAMP_CHECK_CHANGE_REQ:
		CenterToLogic_CampChangeReq(package);
		break;
	default:
		break;
	}
}

void LogicCampModule::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_CREATURE_DEAD:
	{
		if (nLen != sizeof(DieEvent))
		{
			LogErrFmtPrint("[logic] LogicCampModule::OnExecute....nLen != sizeof(DieEvent)....len:%d ", nLen);
			return;
		}
		DieEvent *pEvent = (DieEvent*)pContext;
		if (pEvent)
			OnPlayerDead(pEvent);
	}
	break;
	case EVENT_LEVELUP:
	{
		if (nLen != sizeof(PlayerLeveUpEvent))
		{
			LogErrFmtPrint("[logic] LogicCampModule::OnExecute....nLen != sizeof(PlayerLeveUpEvent)....len:%d ", nLen);
			return;
		}
		PlayerLeveUpEvent *pEvent = (PlayerLeveUpEvent*)pContext;
		if (pEvent)
			OnPlayerLvUp(pEvent);
	}
	break;
	case EVENT_ADD_ATT_POINT:
	{
		if (nLen != sizeof(AddPoingEvent))
		{
			LogErrFmtPrint("[logic] LogicCampModule::OnExecute....nLen != sizeof(AddPoingEvent)....len:%d ", nLen);
			return;
		}
		AddPoingEvent *pEvent = (AddPoingEvent*)pContext;
		if (pEvent)
			OnPlayerAddPoint(pEvent);
	}
	break;
	default:
		break;
	}
}

//客户端请求朝拜属性数据
void LogicCampModule::ClientToLogic_CampPrayAttInfo(CharIDType charId, RECV_PACKAGE &package)
{
	Camp_PrayAttrInfoReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] Camp_PrayAttrInfoReq parse failed...");
		return;
	}

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(charId);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] ClientToLogic_CampPrayAttInfo pPlayer is nullptr...charId:%llu", charId);
		return;
	}

	if (pPlayer->GetAttr(C_CAMP_ID) <= 0)
	{
		Camp_PrayAttrInfoRsp rsp;
		rsp.set_retcode(RET_CAMP_NOT_JOIN_CAMP);
		g_GetLogicService()->SendDataToClient(charId, EMODULE_ID_CAMP, SERVER_TO_CLIENT_CAMP_PRAY_INFO, &rsp);
		return;
	}

	LogicToCenter_PushPrayAtt prayAtt;
	prayAtt.set_char_id(charId);
	prayAtt.set_type(LogicToCenter_PushPrayAtt_Type_PrayInfo);
	prayAtt.set_godid(req.gods_id());

	int64_t att[6] = { 0 };
	pPlayer->GetLevelAddPointAttr(att);
	for (uint32_t i = 0; i != sizeof(att) / sizeof(int64_t); ++i)
	{
		LogicToCenter_PushPrayAtt::Attr *pAttr = prayAtt.add_att();
		if (att == nullptr)		continue;
		pAttr->set_attrid(i + 1);
		pAttr->set_att(att[i]);
	}
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_PUSH_PRAY_INFO, &prayAtt);
}

//客户端请求朝拜
void LogicCampModule::ClientToLogic_CampPrayReq(CharIDType charId, RECV_PACKAGE &package)
{
	Camp_PrayReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] Camp_PrayReq parse failed...charid:%lu ", charId);
		return;
	}
	Camp_PrayRsp rsp;
	rsp.set_retcode(RET_SUCCESS);

	uint32_t godId = req.gods_id();
	uint32_t godType = req.gods_type_id();

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(charId);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::ClientToLogic_CampPrayReq nullptr == pPlayer....charID=%lu", charId);
		return;
	}

	if (pPlayer->GetAttr(C_CAMP_ID) <= 0)
	{
		Camp_PrayAttrInfoRsp rsp;
		rsp.set_retcode(RET_CAMP_NOT_JOIN_CAMP);
		g_GetLogicService()->SendDataToClient(charId, EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_PRAY, &rsp);
		return;
	}

	Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
	if (nullptr == pPackage)
	{
		LogErrFmtPrint("[logic] LogicCampModule::ClientToLogic_CampPrayReq nullptr == pPackage....");
		rsp.set_retcode(RET_FAIL);
		g_GetLogicService()->SendDataToClient(charId, EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_PRAY, &rsp);
		return;
	}

	const CampFeteCfgInfo *pCfg = g_GetCampFeteCfgTable()->GetCampFeteCfgInfo(godType);
	if (nullptr == pCfg)
	{
		LogErrFmtPrint("[logic] LogicCampModule::ClientToLogic_CampPrayReq nullptr == CampFeteCfgInfo *pCfg....godType:%d", godType);
		rsp.set_retcode(RET_CONFIG_ERROR);
		g_GetLogicService()->SendDataToClient(charId, EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_PRAY, &rsp);
		return;
	}


	if (pPlayer->GetAttr(C_DIAMOND) < pCfg->diamondSpend)
	{
		rsp.set_retcode(RET_DIAMOND_LACK);
		g_GetLogicService()->SendDataToClient(charId, EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_PRAY, &rsp);
		return;
	}

	if (pCfg->sacrificeId > 0 && pCfg->sacrificeSpend > 0)
	{
		LIST_ITEM lstRemoveItem;
		SItem item;
		item.nItemID = pCfg->sacrificeId;
		item.nNum = pCfg->sacrificeSpend;
		item.byBind = (int8_t)EBindState::EBindState_all;
		lstRemoveItem.push_back(item);
		if (!pPackage->HasItem(lstRemoveItem))
		{
			rsp.set_retcode(RET_PACKAGE_ITEM_NOT_EXITS);
			g_GetLogicService()->SendDataToClient(charId, EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_PRAY, &rsp);
			return;
		}
	}

	LogicToCenter_PushPrayAtt prayAtt;
	prayAtt.set_char_id(charId);
	prayAtt.set_type(LogicToCenter_PushPrayAtt_Type_PrayReq);
	prayAtt.set_godid(godId);
	prayAtt.set_godtype(godType);
	int64_t att[6] = { 0 };
	pPlayer->GetLevelAddPointAttr(att);
	for (uint32_t i = 0; i != sizeof(att) / sizeof(int64_t); ++i)
	{
		LogicToCenter_PushPrayAtt::Attr *pAttr = prayAtt.add_att();
		if (att == nullptr)		continue;
		pAttr->set_attrid(i + 1);
		pAttr->set_att(att[i]);
	}
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_PUSH_PRAY_INFO, &prayAtt);
}

//中心服通知逻辑服阵营朝拜属性改变
void LogicCampModule::CenterToLogic_CampAttChange(RECV_PACKAGE &package)
{
	LogicToCenter_CampAttChange change;
	if (!change.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] LogicToCenter_CampAttChange parse failed....");
		return;
	}
	CharIDType nCharID = change.char_id();
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(nCharID);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampAttChange pPlayer is nullptr...charId:%llu", nCharID);
		return;
	}
	size_t size = change.att_size();
	for (size_t i = 0; i != size; ++i)
	{
		const LogicToCenter_CampAttChange::Attr &att = change.att(i);
		pPlayer->SetAttrPACache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Camp, att.attrid()), att.att());
	}
	pPlayer->SynAttrToClient();
}

//中心服通知逻辑服玩家阵营信息
void LogicCampModule::CenterToLogic_NotifyCampCharInfo(RECV_PACKAGE &package)
{
	CenterToLogic_CampCharInfo req;
	if (!req.ParseFromArray(package.dataAfterHead,package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CampCharInfo parse failed....");
		return;
	}
	CharIDType nCharID = req.charid();
	uint32_t nSize = req.attr_id_size();

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(nCharID);
	if (nullptr == pPlayer || nSize <= 0)
	{
		return;
	}

	for (uint32_t i = 0; i < nSize; ++i)
	{
		if ((int32_t)pPlayer->GetAttr(req.attr_id(i)) != req.attr_value(i))
		{
			if (req.attr_id(i) == C_CAMP_ID)
			{
				ChangeCampEvent change;
				change.preCamp = pPlayer->GetAttr(C_CAMP_ID);
				change.curCamp = req.attr_value(i);
				change.charId = nCharID;
				pPlayer->SetAttr(req.attr_id(i), req.attr_value(i), false);
				g_GetEvent()->FireExecute(EVENT_CAMP_CHANGE, nCharID, CREATURE_PLAYER, &change, sizeof(ChangeCampEvent));
				pPlayer->AddExp(0);
				if (change.preCamp > 0 && (change.preCamp != change.curCamp))
				{
					LogicToCenter_NotifyEvent notify;
					notify.set_type(ECamp_EventType_LogicChangeCampSuccess);
					notify.set_char_id(nCharID);
					g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_TOTIFY_EVENT, &notify);
				}
			}
			else if (req.attr_id(i) == C_OFFICIAL || req.attr_id(i) == C_KNIGHT)
			{
				pPlayer->SetAttr(req.attr_id(i), req.attr_value(i), false);
				pPlayer->SyncToCenter(req.attr_id(i));
			}
			else
			{
				if (req.attr_id(i) == C_CAMP_SCORE)
				{
					pPlayer->AddSourceAttr(C_CAMP_SCORE, req.attr_value(i) - pPlayer->GetAttr(req.attr_id(i)), false, S_Camp);
				}
				else
				{
					pPlayer->AddAttr(req.attr_id(i), req.attr_value(i) - pPlayer->GetAttr(req.attr_id(i)), false);
				}
			}
		}
	}

	pPlayer->SynAttrToClient();
}
//中心服请求逻辑服验证阵营公告扣费
void LogicCampModule::CenterToLogic_CampCheckNoticeCost(RECV_PACKAGE &package)
{
	CenterToLogic_CheckNoticeCostReq req;
	if (!req.ParseFromArray(package.dataAfterHead,package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CheckNoticeCostReq parse failed....");
		return;
	}
	
	//
	CharIDType ncharID = req.char_id();
	int32_t byCampID = req.camp_id();
	int32_t nDiamond = req.diamond();
	//
	LogicToCenter_CheckNoticeCostRsp rsp;
	rsp.set_retcode(RET_FAIL);
	rsp.set_char_id(ncharID);
	rsp.set_camp_id(byCampID);
	//
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(ncharID);
	if (nullptr != pPlayer)
	{
		if (ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState())
		{
			//切场景状态
			LogErrFmtPrint("CenterToLogic_CampCheckNoticeCost...ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState()...cid:lu,byCampID:%d,nDiamond:%u,zid:%u, id:%u ", ncharID, byCampID, nDiamond, g_GetLogicService()->GetZID(), g_GetLogicService()->GetID());
		}
		else
		{
			int32_t nHas = pPlayer->GetAttr(C_DIAMOND);
			int32_t nHasBind = pPlayer->GetAttr(C_DIAMOND_BIND);
			int32_t nTotal = nHas + nHasBind;
			if (nTotal < nDiamond)
			{
				//钻石不足 RET_DIAMOND_LACK
				rsp.set_retcode(RET_DIAMOND_LACK);
			}
			else
			{
				if (nHasBind >= nDiamond)
				{
					//pPlayer->AddAttr(C_DIAMOND_BIND, -nDiamond, true);
					pPlayer->AddSourceAttr(C_DIAMOND_BIND, -nDiamond, true, S_Camp);
				}
				else
				{
					nDiamond -= nHasBind;
					//pPlayer->AddAttr(C_DIAMOND_BIND, -nHasBind, true);
					pPlayer->AddSourceAttr(C_DIAMOND_BIND, -nHasBind, true, S_Camp);
					//pPlayer->AddAttr(C_DIAMOND, -nDiamond, true);
					pPlayer->AddSourceAttr(C_DIAMOND, -nDiamond, true, S_Camp);
				}
				rsp.set_retcode(RET_SUCCESS);
			}
		}		
	}
	//
	if (!g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_NOTICE_COST_RSP, &rsp))
	{
		LogErrFmtPrint("CenterToLogic_CampCheckNoticeCost...SendDataToCenter failed...cid:lu,byCampID:%d,nDiamond:%u, ret:%d ", ncharID, byCampID, nDiamond, rsp.retcode());
	}
}

//中心服请求逻辑服验证竞选申请扣费
void LogicCampModule::CenterToLogic_CampCheckElectionCost(RECV_PACKAGE &package)
{
	CenterToLogic_CheckElectionCostReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CheckElectionCostReq parse failed....");
		return;
	}

	LogicToCenter_CheckElectionCostRsp rsp;
	rsp.set_retcode(RET_SUCCESS);

	uint64_t ncharID = req.char_id();
	int32_t votesGain = req.votes_gain();
	uint32_t num = req.num();
	uint64_t costGold = votesGain * num;
	int32_t nDiamond = req.diamond();

	rsp.set_char_id(ncharID);
	rsp.set_votes_gain(votesGain);
	rsp.set_num(num);
	rsp.set_declare(req.declare());

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(ncharID);
	if (nullptr != pPlayer)
	{
		if (ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState())
		{
			rsp.set_retcode(RET_FAIL);
			LogErrFmtPrint("CenterToLogic_CampCheckElectionCost...ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState()...cid:%lu ", ncharID);
		}
		else
		{
			int32_t nHasDiamond = pPlayer->GetAttr(C_DIAMOND);
			int64_t nHasGold = pPlayer->GetAttr(C_GOLD);
			if (nHasDiamond < nDiamond)
			{
				rsp.set_retcode(RET_DIAMOND_LACK);
			}
			else if (nHasGold < (int64_t)costGold)
			{
				rsp.set_retcode(RET_GOLD_LACK);
			}
			else
			{
				pPlayer->AddSourceAttr(C_DIAMOND, -nDiamond, true, S_Camp);
				pPlayer->AddSourceAttr(C_GOLD, -1 * (int64_t)costGold, true, S_Camp);

				CampElectionEvent electionEvent;
				electionEvent.type = 2/*竞选*/;
				g_GetEvent()->FireExecute(EVENT_CAMP_ELECTION, pPlayer->GetCid(), pPlayer->CreatureKind(), &electionEvent, sizeof(CampElectionEvent));
			}
		}
		
	}
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_ELECTION_COST_RSP, &rsp);
}

//中心服请求逻辑服验证投票
void LogicCampModule::CenterToLogic_CampCheckVote(RECV_PACKAGE &package)
{
	CenterToLogic_CheckVoteReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CheckVoteReq parse failed....");
		return;
	}

	uint64_t ncharID = req.char_id();
	uint64_t nelectioneerID = req.electioneer_id();

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(ncharID);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampCheckVote nullptr == pPlayer....ncharID=%lu", ncharID);
		return;
	}
	Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
	if (nullptr == pPackage)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampCheckVote nullptr == pPackage....ncharID=%lu", ncharID);
		return;
	}

	const CampConstantCfgMap *pCampConstMap = g_GetCampConstantCfgTable()->GetCampConstantCfgMap();
	if (nullptr == pCampConstMap || pCampConstMap->size() != 1)
	{
		LogErrFmtPrint("[logic]CampMgr::CenterToLogic_CampCheckVote pCampConstMap = nullptr || pCampConstMap->size() != 1.. failed....");
		Camp_VoteRsp rsp;
		rsp.set_retcode(RET_CONFIG_ERROR);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_VOTE, &rsp);
		return;
	}
	const CampConstantCfgInfo *pCampConstCfg = &(pCampConstMap->begin())->second;
	if (nullptr == pCampConstCfg)
	{
		LogErrFmtPrint("[logic]CampMgr::Init CenterToLogic_CampCheckVote = nullptr.. failed....");
		Camp_VoteRsp rsp;
		rsp.set_retcode(RET_CONFIG_ERROR);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_VOTE, &rsp);
		return;
	}

	//投票有概率获得宝箱
	if (Random(10000) <= (uint32_t)pCampConstCfg->CampBallotBoxRand)
	{
		VEC_INT32 vecBox;
		vecBox.clear();
		vecBox.push_back(pCampConstCfg->CampBallotBox);
		SItemSourceParam sourceParam;
		ERetCode result = g_GetDropMgr()->PlayerAddBox(pPlayer, vecBox, sourceParam);
		if (RET_SUCCESS != result)
		{
			Camp_VoteRsp rsp;
			rsp.set_retcode(RET_PACKAGE_SPACE_NOT_ENOUGH);
			g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, CENTER_TO_CLIENT_CAMP_VOTE, &rsp);
			return;
		}
	}

	LogicToCenter_CheckVoteRsp checkRsp;
	checkRsp.set_retcode(RET_SUCCESS);
	checkRsp.set_char_id(ncharID);
	checkRsp.set_electioneer_id(nelectioneerID);
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_VOTE_RSP, &checkRsp);
}

//中心服通知逻辑服朝拜扣除消耗
void LogicCampModule::CenterToLogic_CampCheckPrayCost(RECV_PACKAGE &package)
{
	CenterToLogic_CheckPrayCostReq req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CheckElectionCostReq parse failed....");
		return;
	}

	uint64_t ncharID = req.char_id();
	uint32_t godId = req.gods_id();
	uint32_t godType = req.gods_type();

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(ncharID);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampCheckPrayCost nullptr == pPlayer....charID=%lu", ncharID);
		return;
	}
	Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
	if (nullptr == pPackage)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampCheckPrayCost nullptr == pPackage....");
		return;
	}

	const CampFeteCfgInfo *pCfg = g_GetCampFeteCfgTable()->GetCampFeteCfgInfo(godType);
	if (nullptr == pCfg)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampCheckPrayCost nullptr == CampFeteCfgInfo *pCfg....godType:%d", godType);
		return;
	}

	LogicToCenter_CheckPrayCostRsp rsp;
	rsp.set_retcode(RET_SUCCESS);
	rsp.set_char_id(ncharID);
	rsp.set_gods_id(godId);
	rsp.set_gods_type(godType);

	if (pCfg->diamondSpend > 0 && pPlayer->GetAttr(C_DIAMOND) >= pCfg->diamondSpend)
	{
		//pPlayer->AddAttr(C_DIAMOND, -(int64_t)pCfg->diamondSpend, true);
		pPlayer->AddSourceAttr(C_DIAMOND, -(int64_t)pCfg->diamondSpend, true, S_Camp);
	}

	if (pCfg->sacrificeId > 0 && pCfg->sacrificeSpend > 0)
	{
		LIST_ITEM lstRemoveItem;
		SItem item;
		item.nItemID = pCfg->sacrificeId;
		item.nNum = pCfg->sacrificeSpend;
		item.byBind = (int8_t)EBindState::EBindState_all;
		lstRemoveItem.push_back(item);
		SItemSourceParam soureParam;
		soureParam.source = S_Camp;
		pPackage->RemoveItem(lstRemoveItem, soureParam);
	}

	HuoYueDuPart* pHuoYueDuPart = dynamic_cast<HuoYueDuPart*>(pPlayer->GetPart(PART_HUOYUEDU));
	if (nullptr != pHuoYueDuPart)
	{
		//增加活跃度
		pHuoYueDuPart->AddHuoYueDuRecord(HYDT_CAMP_PRAY);
	}
}

//中心服通知逻辑服加钱币或物品
void LogicCampModule::CenterToLogic_CampAddSth(RECV_PACKAGE &package)
{
	CenterToLogic_NotifyAddSth req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_NotifyAddSth parse failed....");
		return;
	}

	CharIDType ncharID = req.char_id();

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(ncharID);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampAddSth nullptr == pPlayer....charID=%lu", ncharID);
		return;
	}

	//pPlayer->AddAttr(C_GOLD, req.gold(), true);

	pPlayer->AddSourceAttr(C_GOLD, req.gold(), true, S_Camp);

	Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
	if (nullptr == pPackage)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampAddSth nullptr == pPackage....");
		return;
	}

	if (1 == req.operate()/*竞选投票*/)
	{
		CampElectionEvent electionEvent;
		electionEvent.type = req.operate();
		g_GetEvent()->FireExecute(EVENT_CAMP_ELECTION, pPlayer->GetCid(), pPlayer->CreatureKind(), &electionEvent, sizeof(CampElectionEvent));
	}

	LIST_ITEM addItems;
	addItems.clear();
	uint32_t item_size = req.item_id_size();
	for (uint32_t i = 0; i < item_size; ++i)
	{
		SItem item;
		item.nItemID = req.item_id(i);
		item.nNum = req.item_num(i);
		addItems.push_back(item);
	}
	if (addItems.size() > 0)
	{
		SItemSourceParam soureParam;
		soureParam.source = S_Camp;
		if (!pPackage->AddItem(addItems, soureParam))
		{
			VEC_ITEM_PROTO_EX vecProto;
			vecProto.clear();
			SItemCond itemCond;
			g_GetDropMgr()->GetItemCond(pPlayer, itemCond);
			if (g_GetItemMgr()->SetItemProtoInfo(addItems, vecProto, itemCond))
			{
				if (req.operate() == 2)	//天命奖励
				{
					string strTopic = "天命奖励";
					string strContent = "你通过天命选择阵营获得了一份额外奖励，请查收！";
					g_GetGMailMgr()->SendSystemMail(pPlayer->GetId(), strTopic, strContent, 0, 0, 0, 0, 0, vecProto);
				}
				else if (req.operate() == 3)	//爵位分红
				{
					string strTopic = "爵位分红";
					string strContent = "你获得了所在阵营的爵位分红奖励，请查收！";
					g_GetGMailMgr()->SendSystemMail(pPlayer->GetId(), strTopic, strContent, 0, 0, 0, 0, 0, vecProto);
				}
			}
			else
			{
				LogErrFmtPrint("LogicCampModule::CenterToLogic_CampAddSth....g_GetItemMgr()->SetItemProtoInfo failed...cid:%lu,gold:%d,operate:%d ", ncharID, req.gold(), req.operate());
			}			
		}
	}	
}

//中心服通知逻辑服更换阵营标记
void LogicCampModule::CenterToLogic_ChangeCamp(RECV_PACKAGE &package)
{
	CenterToLogic_ChangeCampFlag req;
	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_ChangeCampFlag parse failed....");
		return;
	}

	uint64_t ncharID = req.char_id();
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(ncharID);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_ChangeCamp nullptr == pPlayer....charID=%lu", ncharID);
		return;
	}

/*
	if (req.flag())
	{
		MissionPart* pPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
		if (pPart)
		{
			pPart->AssignMission(change_camp_taskId);
		}
	}*/
}

//中心服通知逻辑服创建/更新阵营首领
void LogicCampModule::CenterToLogic_ChangeCampLeader(RECV_PACKAGE &package)
{
	CenterToLogic_CampLeader campLeader;
	if (!campLeader.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CampLeader parse failed....");
		return;
	}


	uint32_t npcId = campLeader.npc_id();
	//先获得NPC
	Npc* pNpc = g_GetCreatureMgr()->GetNpc(npcId);
	if (pNpc)
	{
		if (campLeader.has_out_ward())
		{
			const CharOutward &outWard = campLeader.out_ward();
			pNpc->SetFacade(outWard);
		}
		else
		{
			//pNpc->DeleteFacade();
			const ArenaRobotCfgInfo* robotCfgInfo = nullptr;
			if (campLeader.camp() == ECamp_Type_Light)
				robotCfgInfo = g_GetArenaRobotCfgTable()->GetArenaRobotCfgInfo(CampLightLeaderNpc);
			else
				robotCfgInfo = g_GetArenaRobotCfgTable()->GetArenaRobotCfgInfo(CampDarkLeaderNpc);

			if (robotCfgInfo == nullptr)
			{
				pNpc->DeleteFacade();
				return;
			}
			CharOutward outWard;
			outWard.set_dstname(robotCfgInfo->robotname);
			outWard.set_sex(robotCfgInfo->sex);
			outWard.set_prof(robotCfgInfo->robotProfession);
			CharFacadeProto *pCharFacade = outWard.mutable_facade();
			if (robotCfgInfo->vec_equip.size() >= 2)
			{
				pCharFacade->set_weapon_id(robotCfgInfo->vec_equip.at(0));
				pCharFacade->set_clothes_id(robotCfgInfo->vec_equip.at(1));
			}
			pNpc->SetFacade(outWard);
		}
	}
	
}

//中心服请求逻辑服验证更换阵营
void LogicCampModule::CenterToLogic_CampChangeReq(RECV_PACKAGE &package)
{
	CenterToLogic_CheckCampChangeReq campChange;
	if (!campChange.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		LogErrFmtPrint("[logic] CenterToLogic_CheckCampChangeReq parse failed....");
		return;
	}

	uint64_t charId = campChange.char_id();
	LogicToCenter_CheckCampChangeRsp rsp;
	rsp.set_char_id(charId);
	rsp.set_retcode(RET_SUCCESS);

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(charId);
	if (nullptr == pPlayer)
	{
		rsp.set_retcode(RET_NOT_HAVE_CHARACTER);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
		return;
	}
	if (ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState())
	{
		rsp.set_retcode(RET_FAIL);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
		LogErrFmtPrint("CenterToLogic_CampChangeReq...ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState()...cid:%lu ", charId);
		return;
	}

	Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
	if (nullptr == pPackage)
	{
		LogErrFmtPrint("[logic] LogicCampModule::CenterToLogic_CampCheckPrayCost nullptr == pPackage....");
		rsp.set_retcode(RET_FAIL);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
		return;
	}

	const CampConstantCfgMap *pCampConstMap = g_GetCampConstantCfgTable()->GetCampConstantCfgMap();
	if (nullptr == pCampConstMap || pCampConstMap->size() != 1)
	{
		LogErrFmtPrint("[logic]CampMgr::CenterToLogic_CampCheckPrayCost pCampConstMap = nullptr || pCampConstMap->size() != 1.. failed....");
		rsp.set_retcode(RET_CONFIG_ERROR);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
		return;
	}
	const CampConstantCfgInfo *pCampConstCfg = &(pCampConstMap->begin())->second;
	if (nullptr == pCampConstCfg)
	{
		LogErrFmtPrint("[logic]CampMgr::Init CenterToLogic_CampCheckPrayCost = nullptr.. failed....");
		rsp.set_retcode(RET_CONFIG_ERROR);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
		return;
	}

	LIST_ITEM lstRemoveItem;
	lstRemoveItem.clear();
	SItem item;
	item.nItemID = pCampConstCfg->ChangeCampIten;
	item.nNum = pCampConstCfg->ChangeCampItenNum;
	item.byBind = (int8_t)EBindState::EBindState_all;
	lstRemoveItem.push_back(item);
	SItemSourceParam sourceParam;
	sourceParam.source = S_Camp;
	if (!pPackage->RemoveItem(lstRemoveItem, sourceParam))
	{
		rsp.set_retcode(RET_CAMP_CHANGE_ITEM_ERROR);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
		return;
	}

	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_CHECK_CHANGE_RSP, &rsp);
}

void LogicCampModule::OnPlayerDead(DieEvent *pEvent)
{
	Player *killer = g_GetCreatureMgr()->GetPlayer(pEvent->killerCid);
	if (nullptr == killer)
		return;
	Player *deader = g_GetCreatureMgr()->GetPlayer(pEvent->nCid);
	if (nullptr == deader)
		return;

	if (killer->InWorldBossStatus() || deader->InWorldBossStatus())		//处于陨落诅咒状态下的玩家 击杀和被击杀相关角色不会增加军功值
		return;

	uint32_t kCampId = killer->GetAttr(C_CAMP_ID);
	uint32_t dCampId = deader->GetAttr(C_CAMP_ID);
	if (kCampId > 0 && dCampId > 0 && kCampId != dCampId)
	{
		LogicToCenter_NotifyEvent notify;
		notify.set_type(ECamp_EventType_Pvp);
		notify.set_char_id(pEvent->killerCid);	//胜者
		notify.set_param1(pEvent->nCid);		//负者
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_TOTIFY_EVENT, &notify);
	}
}

void LogicCampModule::OnPlayerLvUp(PlayerLeveUpEvent *pEvent)
{
	if (pEvent->level < camp_open_level)
	{
		return;
	}

	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(pEvent->cid);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::OnPlayerLvUp pPlayer is nullptr...charId:%llu", pEvent->cid);
		return;
	}

	if (pPlayer->GetAttr(C_CAMP_ID) <= 0)
	{
		return;
	}

	LogicToCenter_PushPrayAtt prayAtt;
	prayAtt.set_char_id(pEvent->cid);
	prayAtt.set_type(LogicToCenter_PushPrayAtt_Type_AttPush);

	int64_t att[6] = { 0 };
	pPlayer->GetLevelAddPointAttr(att);
	for (uint32_t i = 0; i != sizeof(att) / sizeof(int64_t); ++i)
	{
		LogicToCenter_PushPrayAtt::Attr *pAttr = prayAtt.add_att();
		if (att == nullptr)		continue;
		pAttr->set_attrid(i + 1);
		pAttr->set_att(att[i]);
	}
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_PUSH_PRAY_INFO, &prayAtt);
}

void LogicCampModule::OnPlayerAddPoint(AddPoingEvent *pEvent)
{
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(pEvent->charId);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("[logic] LogicCampModule::OnPlayerAddPoint pPlayer is nullptr...charId:%llu", pEvent->charId);
		return;
	}

	if (pPlayer->GetAttr(C_CAMP_ID) <= 0)
	{
		return;
	}

	LogicToCenter_PushPrayAtt prayAtt;
	prayAtt.set_char_id(pEvent->charId);
	prayAtt.set_type(LogicToCenter_PushPrayAtt_Type_AttPush);

	int64_t att[6] = { 0 };
	pPlayer->GetLevelAddPointAttr(att);
	for (uint32_t i = 0; i != sizeof(att) / sizeof(int64_t); ++i)
	{
		LogicToCenter_PushPrayAtt::Attr *pAttr = prayAtt.add_att();
		if (att == nullptr)		continue;
		pAttr->set_attrid(i + 1);
		pAttr->set_att(att[i]);
	}
	g_GetLogicService()->SendDataToCenter(EMODULE_ID_CAMP, LOGIC_TO_CENTER_CAMP_PUSH_PRAY_INFO, &prayAtt);
}
